//<debug>
Ext.Loader.setPath({
    'Ext': 'sdk/src'
});
//</debug>


Ext.application({
    controllers: [
        "Search"
    ],

    name: 'Bible',

    requires: [
        'Ext.MessageBox',
        'Ext.data.reader.Array',
        'Ext.data.Store',
        'Ext.TitleBar',
        'Ext.Picker',

        'Bible.util.Format',
        'Bible.picker.Chapter'
    ],

    views: [
//        'Main',
        'BiblePanel'
    ],

    icon: {
        57: 'resources/icons/Icon.png',
        72: 'resources/icons/Icon~ipad.png',
        114: 'resources/icons/Icon@2x.png',
        144: 'resources/icons/Icon~ipad@2x.png'
    },
    
    phoneStartupScreen: 'resources/loading/Homescreen.jpg',
    tabletStartupScreen: 'resources/loading/Homescreen~ipad.jpg',

    launch: function() {
        // Destroy the #appLoadingIndicator element
        Ext.fly('appLoadingIndicator').destroy();

        // Initialize the main view
//        Ext.Viewport.add(Ext.create('Bible.view.Main'));
        Ext.Viewport.add(Ext.create('Bible.view.BiblePanel'));
    },

    onUpdated: function() {
        Ext.Msg.confirm(
            "Application Update",
            "This application has just successfully been updated to the latest version. Reload now?",
            function() {
                window.location.reload();
            }
        );
    }
});



BibleUtils = {};
(function(){
    var CHAR_START  = '?',
        CHAR_END    = '?',
        enterRegExp      = new RegExp(Ext.String.escapeRegex('n'), 'g'),
        CHAR_STARTRegExp = new RegExp(Ext.String.escapeRegex(CHAR_START), 'g'),
        CHAR_ENDRegExp   = new RegExp(Ext.String.escapeRegex(CHAR_END), 'g');

    Ext.apply(BibleUtils, {
//        /**
//         * renderer Terms Highlight
//         * @param {Object} val
//         * @param {Object} metaData
//         * @param {Ext.data.Record} record
//         * @param {Number} rowIndex
//         * @param {Number} colIndex
//         * @param {Ext.data.Store} store
//         */
//        rendererHighlight : function(val, metaData, record, rowIndex, colIndex, store) {
//            var searchField = store.searchField,
//                showKeywords = store.showKeywords,
//                text = searchField.getRawValue(),
//                split;
//            if(!searchField.hasSearch && !showKeywords){
//                return val;
//            }
//            split = (searchField.split || (searchField.filterType && searchField.store.baseParams.type != 'text'));
//            val = Bible.highlightText(val, text, split, searchField.minChars, showKeywords);
//            return val;
//        },

        /**
         * Escapes diacritics in the passed string for use in a regular expression
         * @param {String} s
         * @return {String}
         */
        escapeDiacriticsRe : function(s){
            return s.replace(/([aâă])/gi, "[aâă]").
                    replace(/([iî])/gi, "[iî]").
                    replace(/([sş])/gi, "[sş]").
                    replace(/([tţ])/gi, "[tţ]");
        },

        /**
         * @param {String} val
         * @param {String/Array} search a String or Array of Strings
         * @param {Boolean} split
         * @return {String} val highlighted value
         */
        highlightText : function(val, search, split, minChars, showKeywords) {
            var //KeywordsStore = Bible.data.KeywordsStore,
                re = [].concat(search);

            minChars = minChars || 1;
            if(split && Ext.isString(search)){
                re = search.split(' ');
            }

//            if(showKeywords){
//                Ext.each(KeywordsStore.keywordsReplace, function(e, i){
//                    val = val.replace(e.replaceReg, e.replaceText);
//                });
//            }

            // highlight
            for(var j=0; j<re.length; j++) {
                if(re[j].length < minChars){
                    continue;
                }
                var s = BibleUtils.escapeDiacriticsRe(Ext.String.escapeRegex(re[j])),
                    regExp = new RegExp('('+s+')', 'gi');
                val = val.replace(regExp, CHAR_START+ "$1" + CHAR_END);
            }

//            if(showKeywords){
//                val = String.format.apply(window, [val].concat(KeywordsStore.keywordsSpan));
//            }

            val = val.replace(CHAR_STARTRegExp, '<span class="termshighlight">');
            val = val.replace(CHAR_ENDRegExp,   '</span>');
            return val;
        }
    });
})();




var booksRecords = [
    ["1", "Genesa","50"],
    ["2", "Exodul","40"],
    ["3", "Leviticul","27"],
    ["4", "Numeri","36"],
    ["5", "Deuteronomul","34"],
    ["6", "Iosua","24"],
    ["7", "Judec\u0103tori","21"],
    ["8", "Rut","4"],
    ["9", "1 Samuel","31"],
    ["10","2 Samuel","24"],
    ["11","1 \u00cemp\u0103ra\u0163i","22"],
    ["12","2 \u00cemp\u0103ra\u0163i","25"],
    ["13","1 Cronici","29"],
    ["14","2 Cronici","36"],
    ["15","Ezra","10"],
    ["16","Neemia","13"],
    ["17","Estera","10"],
    ["18","Iov","42"],
    ["19","Psalmi","150"],
    ["20","Proverbe","31"],
    ["21","Eclesiastul","12"],
    ["22","C\u00e2ntarea c\u00e2nt\u0103rilor","8", "C\u00e2nt. c\u00e2nt\u0103."], // + short name
    ["23","Isaia","66"],
    ["24","Ieremia","52"],
    ["25","Pl\u00e2ngerile lui Ieremia","5", "Pl\u00e2ng. lui Ier."], // + short name
    ["26","Ezechiel","48"],
    ["27","Daniel","12"],
    ["28","Osea","14"],
    ["29","Ioel","3"],
    ["30","Amos","9"],
    ["31","Obadia","1"],
    ["32","Iona","4"],
    ["33","Mica","7"],
    ["34","Naum","3"],
    ["35","Habacuc","3"],
    ["36","\u0162efania","3"],
    ["37","Hagai","2"],
    ["38","Zaharia","14"],
    ["39","Maleahi","4"],
    ["40","Matei","28"],
    ["41","Marcu","16"],
    ["42","Luca","24"],
    ["43","Ioan","21"],
    ["44","Faptele Apostolilor","28", "Fapte"], // + short name
    ["45","Romani","16"],
    ["46","1 Corinteni","16"],
    ["47","2 Corinteni","13"],
    ["48","Galateni","6"],
    ["49","Efeseni","6"],
    ["50","Filipeni","4"],
    ["51","Coloseni","4"],
    ["52","1 Tesaloniceni","5"],
    ["53","2 Tesaloniceni","3"],
    ["54","1 Timotei","6"],
    ["55","2 Timotei","4"],
    ["56","Tit","3"],
    ["57","Filimon","1"],
    ["58","Evrei","13"],
    ["59","Iacov","5"],
    ["60","1 Petru","5"],
    ["61","2 Petru","3"],
    ["62","1 Ioan","5"],
    ["63","2 Ioan","1"],
    ["64","3 Ioan","1"],
    ["65","Iuda","1"],
    ["66","Apocalipsa","22"]
];

//var books = Ext.create('Ext.data.Store', {
//    //model: 'Bible.model.Books',
//
//    fields: [
//        {name: 'id',   type: 'int'},
//        {name: 'name', type: 'string'},
//        {name: 'chapters', type: 'int'}
//    ],
//
//    data: booksRecords
//});